package com.ding;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.opensymphony.xwork2.ActionSupport;
import com.util.ExcelBean;
import com.util.ExcelUtil;
import com.util.FileUtil;


public class CheckBoxTest2Action extends ActionSupport{
	
	private static final long serialVersionUID = 2451210749370161086L;
	private String copyPath;
	private String toCopyPath;//考贝文件到某个目录下的路径

	private String[] magazines;//选定的文件类型
	private boolean hiddenFile;//是否顯示隱藏文件
    private int countFile;//記錄文件個數
    private String sel;
    
    private String date;

	private List filePathList = new ArrayList();
	
    public List<Magazine> getMagazineList() {
        List<Magazine> magazines = new ArrayList<Magazine>();
        magazines.add(new Magazine("034", "The Economist"));
        magazines.add(new Magazine("122", "Business Week"));
        magazines.add(new Magazine("434", "Fortune"));
        magazines.add(new Magazine("906", "Small Business"));
        return magazines;
    }
    

    
    public void setMagazines(String[] codes) {
        for (String code : codes) {
            System.out.println(code + " is selected");
        }
        magazines  = codes;
        
    }






	public String getDate() {
		return date;
	}



	public void setDate(String date) {
		this.date = date;
	}



	public List getFilePathList() {
		return filePathList;
	}



	public void setFilePathList(List filePathList) {
		this.filePathList = filePathList;
	}



	public int getCountFile() {
		return countFile;
	}



	public void setCountFile(int countFile) {
		this.countFile = countFile;
	}



	@Override
	public String execute() throws Exception {
		Date dirModifyTime  ;//这个是接收前台控制文件修改时间
		
		String lastDirName = copyPath.substring(copyPath.lastIndexOf("\\")+1);//最后一个目录名
		if(date == null || "".equals(date)){
			dirModifyTime = new Date();//如果前台接收的日期为空则给定当前时间
		}else{
			dirModifyTime = DateUtil.getDate(date);
		}
		
		System.out.println(magazines);
		File file=new File(toCopyPath);
		if(file != null && file.exists()){
			try {
				FileUtil.delAll(file);//删除将要考贝到的目录下的文件
			} catch (Exception e) {
				this.addActionError("不能刪除有進程正在使用,請查看");
				e.printStackTrace();
				System.out.println("不能刪除有進程正在使用");
				return "input";
			}
			
			
		}
    	FileUtil.ergodicAndCopyFile(lastDirName ,copyPath ,toCopyPath ,dirModifyTime ,hiddenFile,magazines);//拷贝出按修改时间比较出的文件
        
		
		Map<String , List<String>> map = FileUtil.ergodicFile(toCopyPath);//写出路径
		
		countFile = 0;//统计文件个数
		String varName = sel.toLowerCase();//A--allPath  or N---simpleName (全转换成小写)(控件器)
		for (Iterator iterOut = map.entrySet().iterator(); iterOut.hasNext();) {
			Map.Entry<String, List> entry = (Map.Entry<String, List>) iterOut.next();
			String suffix = entry.getKey();
			List<String> listPath =  entry.getValue();
			countFile =countFile+ listPath.size();
			
			filePathList.addAll(genenaiPathAandN(listPath, varName, lastDirName));//生成前台顯示的數據
			
		}
		
		
			
		System.out.println("总共有 "+countFile+"个文件");
		
		return super.execute();
	}
	
//	生成前台顯示路徑數據
	public List genenaiPathAandN(List<String> listPath, String varName ,String lastDirName){
		List getFilePathList = new ArrayList();
		for (Iterator iterIn = listPath.iterator(); iterIn.hasNext();) {
			String element = (String) iterIn.next();
			if("A".equalsIgnoreCase(varName)){//找出全称路径
				
				getFilePathList.add(element.substring(element.indexOf(lastDirName)));//全部
				//System.out.println(element.substring(element.indexOf(lastDirName)));
			}else if("N".equalsIgnoreCase(varName)){
				getFilePathList.add(element.substring(element.lastIndexOf("\\")+1));//尾
				//System.out.println(element.substring(element.lastIndexOf("\\")+1));//尾
			}else{
				if(element.lastIndexOf(varName) != -1){
					getFilePathList.add(element.substring(element.indexOf(lastDirName)));
					//System.out.println(element.substring(element.indexOf(lastDirName)));
				}
				
			}
		
		}
		return getFilePathList;
	}


	public String getCopyPath() {
		return copyPath;
	}

	public String getToCopyPath() {
		return toCopyPath;
	}

	public void setCopyPath(String copyPath) {
		this.copyPath = copyPath;
	}


	public void setToCopyPath(String toCopyPath) {
		this.toCopyPath = toCopyPath;
	}

	public String[] getMagazines() {
		return magazines;
	}

	public boolean isHiddenFile() {
		return hiddenFile;
	}

	public void setHiddenFile(boolean hiddenFile) {
		this.hiddenFile = hiddenFile;
	}



	public String getSel() {
		return sel;
	}



	public void setSel(String sel) {
		this.sel = sel;
	}
    
    
}

class Magazine {
    private String code;
    private String name;
    public Magazine(String code, String name) {
        this.code = code;
        this.name = name;
    }
    public String getCode() {
        return code;
    }
    public String getName() {
        return name;
    }
}
